package ru.mera
import grails.converters.*
class EmstreeController {
    def index = {
		render("WEEEEEEEE!");
	}
	def lookattree = {

	}
	def nicejson = {
		loadTestData();
		def tojson = Employee.findAll();
		def t = [['id': 1, 'text': 'child 1','leaf': 'true'],
				 ['id': 2, 'text': 'child 2','leaf': 'true'],
				 ['id': 3, 'text': 'fold','children': 
					 ['id': 4, 'text': 'child 4','leaf': 'true']]];
		render t as JSON
		//render ("<i>There must be nice json</i><br>Hey, look: ${params.val}");
	}
	private void loadTestData() {
		def c1 = new Company(name: 'My company', desc: 'qqq www eee').save();
		def d1 = new Department(name: 'My Department 1', desc: 'qqqq').save();
		def d2 = new Department(name: 'My Department 2', desc: 'wwww').save();
		def p1 = new Project(name: 'My Project 1', desc: 'eeee').save();
		def p2 = new Project(name: 'My Project 2', desc: 'rrrr').save();
		def e1 = new Employee(firstName: 'Jan', lastName: 'Fith', email: 'qq@ww.ee', desc: 'tttt').save();
		def e2 = new Employee(firstName: 'Stive', lastName: 'Black', email: 'sb@ww.ee', desc: 'nnnn').save();
		def e3 = new Employee(firstName: 'Mark', lastName: 'With', email: 'dfq@ww.ee', desc: 'tsdfsdftt').save();
		def e4 = new Employee(firstName: 'Tom', lastName: 'Red', email: 'sdfq@ww.ee', desc: 'ttdsfst').save();
		def e5 = new Employee(firstName: 'Ann', lastName: 'Green', email: 'qq@wewrwe.ee', desc: 'tsdfttt').save();
		
		c1.addToDepartments(d1);
		c1.addToDepartments(d2);
		
		d1.addToProjects(p1);
		d1.addToProjects(p2);
		
		p1.addToEmployees(e1);
		p1.addToEmployees(e2);
		p1.addToEmployees(e3);
		p1.addToEmployees(e4);
		p2.addToEmployees(e5);
	}
}
